import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class SnakePanel extends JPanel implements KeyListener, ActionListener {
    ImageIcon up = new ImageIcon("up.png");
    ImageIcon down = new ImageIcon("down.png");
    ImageIcon left = new ImageIcon("left.png");
    ImageIcon right = new ImageIcon("right.png");
    ImageIcon title = new ImageIcon("title.png");
    ImageIcon food = new ImageIcon("food.png");
    ImageIcon body = new ImageIcon("body.png");

    int[] snakex = new int[750];
    int[] snakey = new int[750];
    int len = 3;
    String direction = "R";
    boolean isStarted = false;
    boolean isFail = false;
    Timer timer = new Timer(200, this);

    Random random = new Random();
    int foodx = random.nextInt(34) * 25 + 25;
    int foody = random.nextInt(24) * 25 + 75;
    int score = 0;

    public SnakePanel() {
        this.setFocusable(true);
        initSnake();
        this.addKeyListener(this);
        timer.start();
    }

    public void initSnake() {
        len = 3;
        direction = "R";
        snakex[0] = 100;
        snakey[0] = 100;
        snakex[1] = 75;
        snakey[1] = 100;
        snakex[2] = 50;
        snakey[2] = 100;
        isStarted = false;
        isFail = false;

    }

    public void paint(Graphics graphics) {
//        this.setBackground(Color.BLACK);
        this.setBackground(Color.CYAN);
        graphics.fillRect(25, 75, 850, 600);
        title.paintIcon(this, graphics, 25, 11);

        if (direction.equals("R")) {
            right.paintIcon(this, graphics, snakex[0], snakey[0]);
        } else if (direction.equals("L")) {
            left.paintIcon(this, graphics, snakex[0], snakey[0]);
        } else if (direction.equals("U")) {
            up.paintIcon(this, graphics, snakex[0], snakey[0]);
        } else if (direction.equals("D")) {
            down.paintIcon(this, graphics, snakex[0], snakey[0]);
        }

        for (int i = 1; i < len; i++) {
            body.paintIcon(this, graphics, snakex[i], snakey[i]);
        }
        if (!isStarted) {
            graphics.setColor(Color.WHITE);
            graphics.setFont(new Font("arial", Font.BOLD, 30));
            graphics.drawString("Press Space to Start/Pause", 300, 300);
        }
        if (isFail) {
            graphics.setColor(Color.WHITE);
            graphics.setFont(new Font("arial", Font.BOLD, 30));
            graphics.drawString("Game Over Press Space to Start", 300, 300);
        }
        food.paintIcon(this, graphics, foodx, foody);
        graphics.setColor(Color.CYAN);
        graphics.setFont(new Font("arial", Font.PLAIN, 16));
        graphics.drawString("Score:" +score, 750, 30);
        graphics.drawString("length:" +len, 750, 50);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //空格键控制
        if (keyCode == KeyEvent.VK_SPACE) {
            if (isFail) {
                initSnake();
            } else {
                isStarted = !isStarted;
            }

//            repaint();
        } else if (keyCode == KeyEvent.VK_UP && !direction.equals("D")) {
            direction = "U";
        } else if (keyCode == KeyEvent.VK_DOWN && !direction.equals("U")) {
            direction = "D";
        } else if (keyCode == KeyEvent.VK_LEFT && !direction.equals("R")) {
            direction = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT && !direction.equals("L")) {
            direction = "R";

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();
        if (isStarted && !isFail) {
            for (int i = len; i > 0; i--) {
                snakex[i] = snakex[i - 1];
                snakey[i] = snakey[i - 1];
            }
            if (direction.equals("R")) {
                snakex[0] = snakex[0] + 25;
                if (snakex[0] > 850) {
                    snakex[0] = 25;
                }
            } else if (direction.equals("L")) {
                snakex[0] = snakex[0] - 25;
                if (snakex[0] < 25) {
                    snakex[0] = 850;
                }
            } else if (direction.equals("U")) {
                snakey[0] = snakey[0] - 25;
                if (snakey[0] < 75) {
                    snakey[0] = 650;
                }
            } else if (direction.equals("D")) {
                snakey[0] = snakey[0] + 25;
                if (snakey[0] > 650) {
                    snakey[0] = 75;
                }
            }
            if (snakex[0] == foodx && snakey[0] == foody) {
                len++;
                foodx = random.nextInt(34) * 25 + 25;
                foody = random.nextInt(24) * 25 + 75;
                score++;
            }
            for (int i = 1; i < len; i++) {
                if (snakex[0] == snakex[i] && snakey[0] == snakey[i]) {
                    isFail = true;
                }
            }
        }
        repaint();
    }
}

